

/* 
		Filename:  our_fstream.h
		Created Date:  2/18/11
		Author:      Jason Frank

		Purpose:  This class extends C++'s fstream class to better fit what we need for our file system
		          I/O.  It will abstract away a lot of painful details that we must do just to do
							basic reads and write to/from our "disk".  

							Such tasks include bit-shifting and byte-swapping, to enable 4-bytes ints to be
							properly written to disk, and to deal with Big-endian/Little-endian swapping
							issues that we have found to be a problem.

							Another common thing that needs to be done with reads and writes is to check for 
							errors after every little mini-step (check after seeking, check after writing,
							check after flushing...).  The functions in this class check for all such errors.
							Therefore, most functions take the calling function's name as a parameter, so that 
							the functions of this object can report to the user where the error occurred. 

*///

#include <fstream>
#include <vector>
#include <iostream>
#include <string>

using namespace std;

#define WINDOWS_DEV true // later we can set it to false


class our_fstream : public fstream  {

	public:
		////////////////////////////
		///// OVERRIDDEN METHODS //// (Same fuctionality as super class's, but also does error checking, etc)

		// seekp() and seekp()
		istream& seekg( streamoff offset, ios_base::seekdir dir, string calling_func_name );
		ostream& seekp( streamoff offset, ios_base::seekdir dir, string calling_func_name );
		// write()
		ostream& write(const char* s, streamsize n, string calling_func_name );
		// read()
		istream& read( char* s, streamsize n, string calling_func_name);


		///////////////////////////
		///// ADDED METHODS ///////

		void int_to_4byte_char(unsigned int val, char* cstr, string calling_func_name );
		// to deal with big-endian/little-endian byte swapping
		unsigned int reverse_4bytes(unsigned int val, string calling_func_name );
		// fread():  seek to position (to offset from beg of file), then read num_bytes into buf
		istream& fread( int offset, char* buf, streamsize num_bytes, string calling_func_name);
		// type_read(): Completely handles getting 4 bytes off of disk (at "offset") and putting them into "int_var_ptr" 
		istream& type_read( int offset, unsigned int* int_var_ptr, string calling_func_name);
		istream& type_read_at_cur_pos(  unsigned int* int_var_ptr, string calling_func_name);

		// fwrite():  seek to position (to offset from beg of file), then write num_bytes from buf to disk (flush too)
		ostream& fwrite( int offset, const char* buf, streamsize num_bytes, string calling_func_name);
		// type_write()
		ostream& type_write( int offset, unsigned int var, string calling_func_name);
		ostream& type_write( int offset, const vector<unsigned int>& vec, string calling_func_name);
		ostream& type_write_at_cur_pos(  unsigned int var, string calling_func_name);

}; ////////// end class our_fstream ///////////